//
//  UIAlertController+ACExt.m
//  ACFramework
//
//  Created by caoyq on 2018/5/23.
//

#import "UIAlertController+ACExt.h"

@implementation UIAlertController (ACExt)

#pragma mark - Alert

/**
 * 快速创建AlertController，采用默认的控制器
 */
+ (void)ac_showAlertWithTitle: (NSString *)title message: (NSString *)message actionTitles: (NSArray<NSString *> *)actions cancelTitle: (NSString *)cancelTitle style: (UIAlertControllerStyle)style completion: (void(^)(NSInteger index))completion {
    UIViewController *vc = [[UIApplication sharedApplication] keyWindow].rootViewController;
    [self ac_showAlertWithTitle:title message:message actionTitles:actions cancelTitle:cancelTitle style:style superViewController:vc completion:completion];
}

/**
 * 快速创建AlertController，自定义控制器
 */
+ (void)ac_showAlertWithTitle: (NSString *)title message: (NSString *)message actionTitles: (NSArray<NSString *> *)actions cancelTitle: (NSString *)cancelTitle style: (UIAlertControllerStyle)style superViewController: (UIViewController *)vc completion: (void(^)(NSInteger index))completion
{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:style];
    for (NSInteger index = 0; index < actions.count; index++) {
        UIAlertAction *action = [UIAlertAction actionWithTitle:actions[index] style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            if (completion) {
                completion(index);
            }
        }];
        [alert addAction:action];
    }
    if (cancelTitle.length) {
        UIAlertAction *cancel = [UIAlertAction actionWithTitle:cancelTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            if (completion) {
                completion(-1);
            }
        }];
        [alert addAction:cancel];
    }
    [vc presentViewController:alert animated:YES completion:nil];
}

#pragma mark - Alert Textfield

+ (void)ac_showInputAlertWithTitle: (NSString *)title message: (NSString *)message actionTitle: (NSString *)actionTitle cancelTitle: (NSString *)cancelTitle placeholders: (NSArray *)placeholders completion: (void(^)(NSArray *list))completion {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action = [UIAlertAction actionWithTitle:actionTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        if (completion) {
            NSMutableArray *ary = [NSMutableArray array];
            for (UITextField *textF in alert.textFields) {
                [ary addObject:textF.text];
            }
            completion(ary);
        }
    }];
    action.enabled = NO;
    [alert addAction:action];
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:cancelTitle style:UIAlertActionStyleCancel handler:nil];
    [alert addAction:cancel];
    for (NSInteger index = 0; index < placeholders.count; index ++) {
        [alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
            textField.placeholder = placeholders[index];
            if (index == 1) {
                textField.keyboardType = UIKeyboardTypeNumberPad;
            }
            [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChange) name:UITextFieldTextDidChangeNotification object:nil];
        }];
    }
    UIViewController *vc = [[UIApplication sharedApplication] keyWindow].rootViewController;
    [vc presentViewController:alert animated:YES completion:nil];
}

#pragma mark - private

/** UITextField 监控文字输入 通知接收方法 */
+ (void)textChange {
    UIViewController *vc = [[UIApplication sharedApplication] keyWindow].rootViewController;
    UIAlertController *alert = (UIAlertController *)vc.presentedViewController;
    if (alert) {
        UIAlertAction *action = alert.actions.firstObject;
        NSInteger count = alert.textFields.count;
        NSInteger number = 0;
        for (NSInteger index = 0; index < count; index ++) {
            if (alert.textFields[index].text.length > 0) {
                number += 1;
            }else {
                break;
            }
        }
        action.enabled = number == count ? YES : NO;
    }
}

@end
